/**
* @file         demo_create_task_use_params.c
* @author       David Hu (hmd_hubei_cn@163.com)
* @brief         
* @version      0.1
* @date         2023.12.16
* @note          
* @copyright    Copyright (c) 2022 DAVID HU All rights reserved. Licensed under the MIT License (the "License");

* you may not use this file except in compliance with the License.

* You may obtain a copy of the License in the file LICENSE

* Unless required by applicable law or agreed to in writing, software

* distributed under the License is distributed on an "AS IS" BASIS,

* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

* See the License for the specific language governing permissions and

* limitations under the License.
**/

#include "demo_create_task_use_params.h"

#include <rtthread.h>
#include <rtdevice.h>

#define THREAD_PRIORITY         15  //设置线程优先级
#define THREAD_STACK_SIZE       512 //设置线程栈大小
#define THREAD_TIMESLICE        15  //设置线程时间片大小

static char thread1_stack[1024];    //设置线程1的内存栈
static struct rt_thread thread1;    //定义线程1句柄

static struct rt_thread *thread2;   //定义线程2句柄指针


/* 线程的入口函数 */
static void thread1_entry(void *parameter)
{
	const char *thread_name = parameter;
	volatile rt_uint32_t cnt = 0;
	
	/* 线程 */
	while(1)
	{
		/* 打印线程的信息 */
		rt_kprintf(thread_name);
		
		/* 延迟一会(比较简单粗暴) */
		for( cnt = 0; cnt < 100000; cnt++ )
		{
		}
	}
}

static const char *thread1_name = "OS: Thread1 run\r\n";
static const char *thread2_name = "OS: Thread2 run\r\n";

int func_demo_create_task_use_params(void) {
  /* 初始化静态线程1，名称是Thread1，入口是thread1_entry */
    rt_thread_init(&thread1,               //线程句柄 
                   "thread1",              //线程名字
                   thread1_entry,          //入口函数
                   (void *)thread1_name,   //入口函数参数
                   &thread1_stack[0],      //线程栈起始地址
                   sizeof(thread1_stack),  //栈大小
                   THREAD_PRIORITY,        //线程优先级
				   THREAD_TIMESLICE);      //线程时间片大小     
	/* 启动线程1 */
    rt_thread_startup(&thread1);           
	
	
	/* 创建动态线程2，名称是thread2，入口也是thread1_entry*/
    thread2 = rt_thread_create("thread2",          //线程名字
                            thread1_entry,         //入口函数
							(void *)thread2_name,  //入口函数参数
                            THREAD_STACK_SIZE,     //栈大小
                            THREAD_PRIORITY,       //线程优先级
				            THREAD_TIMESLICE);     //线程时间片大小

    /* 判断创建结果,再启动线程2 */
    if (thread2 != RT_NULL)
        rt_thread_startup(thread2);	
  return 0;
}

/********************* (C) COPYRIGHT DAVID HU *******END OF FILE ********/
